using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Text;
using System.Diagnostics;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Card;
using MaskData;

namespace MaskMgr
{
    internal class ViewCreator
    {
		private ViewCreator()
		{}
		private static ViewCreator _Instance = null;
		private Dictionary<String, List<GridColumn>> _ColumnsMap = new Dictionary<string, List<GridColumn>>();
		public static ViewCreator Instance()
		{
			if (_Instance == null)
				_Instance = new ViewCreator();
			return _Instance;
		}
		public void GetViewColumns(MaskNodes nodes,
			CardView cardView,
			RepositoryItemPictureEdit pictureEdit)
		{
			List<GridColumn> columns = null;
			try
			{
				columns = _ColumnsMap[nodes.MaskNodeID];
			}
			catch (KeyNotFoundException e)
			{
				columns = null;
			}
			if (columns == null)
			{
				columns = CreateViewColumns(nodes.ListTypes, pictureEdit);
				_ColumnsMap[nodes.MaskNodeID] = columns;
			}

			cardView.Columns.Clear();
			foreach (GridColumn column in columns)
			{
				cardView.Columns.AddRange(new GridColumn[] {
				column});
			}
		}
		private List<GridColumn> CreateViewColumns(List<ElementType> types, RepositoryItemPictureEdit pictureEdit)
		{
			List<GridColumn> columns = new List<GridColumn>();
			foreach (ElementType type in types)
			{
				if (type.IsDisplay == false)
					continue;
				GridColumn gridColumn1 = new GridColumn();
				gridColumn1.Caption = type.Caption;
				gridColumn1.FieldName = type.FieldName;
				gridColumn1.Visible = type.IsDisplay;
				gridColumn1.VisibleIndex = type.Order;
				if (type is ImageElementType)
					gridColumn1.ColumnEdit = pictureEdit;
				else
				{
					gridColumn1.OptionsColumn.AllowEdit = false;
					gridColumn1.OptionsColumn.ReadOnly = true;
				}

				columns.Add(gridColumn1);
			}
			return columns;
		}
		/// <summary>
		/// generate all the columns of card view
		/// </summary>
		/// <param name="types">the types of columns</param>
		/// <param name="cardView">card view</param>
		/// <param name="pictureEdit">if all the types of columns contains a image type, we need it</param>
		private void GenerateColumns(List<ElementType> types,
			CardView cardView,
			RepositoryItemPictureEdit pictureEdit)
        {
			cardView.Columns.Clear();

            foreach (ElementType type in types)
            {
				if (type.IsDisplay == false)
					continue;
                GridColumn gridColumn1 = new GridColumn();
                gridColumn1.Caption = type.Caption;
                gridColumn1.FieldName = type.FieldName;
                gridColumn1.Visible = type.IsDisplay;
                gridColumn1.VisibleIndex = type.Order;
				if (type is ImageElementType)
					gridColumn1.ColumnEdit = pictureEdit;
				else {
					gridColumn1.OptionsColumn.AllowEdit = false;
					gridColumn1.OptionsColumn.ReadOnly = true;
				}

				cardView.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] {
                gridColumn1});
            }


        }
		/// <summary>
		/// generate tree view
		/// </summary>
		/// <param name="treeList">display tree view</param>
		/// <param name="root">the root of tree node</param>
		/// <param name="ParentNode">parent node in display view</param>
		public void GenerateTreeView(TreeList treeList, NavigateTreeNode root, TreeListNode ParentNode)
		{
			foreach (NavigateTreeNode node in root.Children)
			{
				TreeListNode parentnode = treeList.AppendNode(new object[] { node.Name }, ParentNode);
				parentnode.Tag = node;
				GenerateTreeView(treeList, node, parentnode);
			}
		}
    }
}